package com.iweb.day09;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * @author WuYongheng
 * @date 2021/11/15
 * @description
 */
public class TestPerson {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("001", "jerry"));
        personList.add(new Person("002", "tom"));
        personList.add(new Person("003", "kitty"));
        personList.add(new Person("004", "sam"));
        personList.add(new Person("005", "tony"));
        for (Object name : personList) {
            System.out.print(name + " ");
        }
        System.out.println();
        Object o = personList.get(2).name;
        System.out.println("第三个元素的name为：" + o);
        // 通过下标删除
        personList.remove(personList.size() - 1);

        // 遍历 Iterator
        // 获取personList的迭代器
        System.out.println("迭代器遍历");
        Iterator<Person> iterator = personList.iterator();
        // 通过迭代器进行迭代操作
        while (iterator.hasNext()) {
            Person key = iterator.next();
            System.out.print(key.name + " ");
        }
        System.out.println();
        System.out.println("forEach 遍历");
        // forEach 遍历
        for (Person p : personList) {
            System.out.print(p.name + " ");
        }


    }
}
